import dayjs from "dayjs";
// import flowLimitsEzxample from "./flowLimitsEzxample.json";

export default function (
  begTime = dayjs().format("YYYY-MM-DD HH:mm:s"),
  flowDataHalf = [],
  flowDataFull = [],
  flowTags = [],
  flowLimits = [],
  stepMinute = 30,
  rangeHour = 6
  // flowLimits = flowLimitsEzxample
) {
  if (!begTime || !flowDataHalf || !flowDataFull || !flowTags || !flowLimits) {
    return [];
  }
  let result = [];
  let begTimeObj = dayjs(begTime);
  for (let i = 0; i < flowLimits.length; i++) {
    let targetLimit = flowLimits[i];
    let tagIDs = [];
    // 筛选需要累加的tagID
    for (let j = 0; j < flowTags.length; j++) {
      for (let k = 0; k < targetLimit.entries.length; k++) {
        if (flowTags[j].includes(targetLimit.entries[k])) {
          tagIDs.push(j);
        }
      }
    }

    for (let j = 0; j < flowDataHalf.length; j++) {
      let current = dayjs(begTime).add(stepMinute * j, "minute");
      let hoursDiff = dayjs().diff(current, "hour");
      if (hoursDiff >= 0 || hoursDiff <= rangeHour) {
        // 遍历flowData
        let valueHalf = 0;
        let valueFull = 0;
        let details = [];
        for (let k = 0; k < tagIDs.length; k++) {
          details.push({
            index: j,
            tag: flowTags[tagIDs[k]],
            value1: flowDataHalf[j][tagIDs[k]],
            value2: flowDataFull[j][tagIDs[k]],
          });
          valueHalf += flowDataHalf[j][tagIDs[k]];
          valueFull += flowDataFull[j][tagIDs[k]];
        }
        if (targetLimit.limits.half.enabled) {
          // Half hour limits check
          if (valueHalf >= targetLimit.limits.half.danger) {
            // danger value
            let from = begTimeObj.add(stepMinute * j, "minute");
            let to = begTimeObj.add(stepMinute * j + 30, "minute");
            result.push({
              from,
              // fromString: from.format("YYYY-MM-DD HH:mm:ss"),
              to,
              // toString: to.format("YYYY-MM-DD HH:mm:ss"),
              value: valueHalf,
              type: "danger",
              limit: targetLimit.limits.half.danger,
              remark: targetLimit.remark,
              details,
            });
          } else if (valueHalf >= targetLimit.limits.half.warning) {
            // warning value
            let from = begTimeObj.add(stepMinute * j, "minute");
            let to = begTimeObj.add(stepMinute * j + 30, "minute");
            result.push({
              from,
              // fromString: from.format("YYYY-MM-DD HH:mm:ss"),
              to,
              // toString: to.format("YYYY-MM-DD HH:mm:ss"),
              value: valueHalf,
              type: "warning",
              limit: targetLimit.limits.half.danger,
              remark: targetLimit.remark,
              details,
            });
          }
        }
        if (targetLimit.limits.full.enabled) {
          // Full hour limits check
          if (valueFull >= targetLimit.limits.full.danger) {
            let from = begTimeObj.add(stepMinute * j, "minute");
            let to = begTimeObj.add(stepMinute * j + 60, "minute");
            result.push({
              from,
              // fromString: from.format("YYYY-MM-DD HH:mm:ss"),
              to,
              // toString: to.format("YYYY-MM-DD HH:mm:ss"),
              value: valueFull,
              type: "danger",
              limit: targetLimit.limits.full.danger,
              remark: targetLimit.remark,
              details,
            });
          } else if (valueFull >= targetLimit.limits.full.warning) {
            let from = begTimeObj.add(stepMinute * j, "minute");
            let to = begTimeObj.add(stepMinute * j + 60, "minute");
            result.push({
              from,
              // fromString: from.format("YYYY-MM-DD HH:mm:ss"),
              to,
              // toString: to.format("YYYY-MM-DD HH:mm:ss"),
              value: valueFull,
              type: "warning",
              limit: targetLimit.limits.full.danger,
              remark: targetLimit.remark,
              details,
            });
          }
        }
        //遍历结束
      }
    }
  }
  result.sort((a, b) => a.from.unix() - b.from.unix());
  return result;
}
